package com.zx.platform.dao.impl;

import com.zx.platform.dao.MenusDao;
import com.zx.platform.dao.mapper.MenusMapper;
import com.zx.platform.dao.mapper.RoleMenusMapper;
import com.zx.platform.dao.model.Menus;
import com.zx.platform.dao.model.MenusExample;
import com.zx.platform.dao.model.RoleMenus;
import com.zx.platform.dao.model.RoleMenusExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 14-3-1
 * Time: 上午10:54
 * To change this template use File | Settings | File Templates.
 */
@Component("MenusDao")
public class MenusImpl implements MenusDao {

    @Autowired
    private MenusMapper mapper;
    
    @Autowired
    private RoleMenusMapper roleMenusMapper;

    /**
     * 新增
     */
    public Integer add(Menus menus) throws DataAccessException {
        int column = mapper.insert(menus);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return menus.getMenuId();
    }

    /**
     * 修改
     * @param menus
     * @return
     * @throws DataAccessException
     */
    public Integer update(Menus menus) throws DataAccessException {
        int column = mapper.updateByPrimaryKeySelective(menus);
        if (column != 1) {
            throw new RuntimeException("");
        }
        return menus.getMenuId();
    }

    /**
     * 删除
     */
    public void delete(Menus menus) throws DataAccessException {
        int column = mapper.deleteByPrimaryKey(menus.getMenuId());
        if (column != 1) {
            throw new RuntimeException("");
        }
    }

    /**
     * 分页查找
     */
	public List<Menus> getPageByExample(int pageSize, int pageBegin,
			MenusExample example) throws DataAccessException {
		return mapper.getPageByModel(pageSize, pageBegin, example); 
	}
	/**
	 * 查找条数
	 * 
	 * @param example
	 * @return
	 * @throws DataAccessException
	 */
	public int countByExample(MenusExample example) throws DataAccessException {
		return mapper.countByExample(example);
	}

	/**
	 * 
	 */
	public List<Menus> getListByExample(MenusExample example)
			throws DataAccessException {
		if(example==null){
			example= new MenusExample();
		}
		example.setOrderByClause("orders");
		return mapper.selectByExample(example); 
	}

	public Menus selectByPrimaryKey(Integer key) throws DataAccessException {
		return mapper.selectByPrimaryKey(key);
	}
	/**
	 * 根据roleId获得相关联的menuList
	 * @param roleId
	 * @return
	 * @throws DataAccessException
	 */
	public List getMenusByRoleId(Integer roleId) throws DataAccessException {
		RoleMenusExample roleMenusExample = new RoleMenusExample();
		com.zx.platform.dao.model.RoleMenusExample.Criteria criteria = roleMenusExample.createCriteria();
		criteria.andRoleIdEqualTo(roleId);
		//查出关联表
		List<RoleMenus> roleMenusList = roleMenusMapper.selectByExample(roleMenusExample);
		List<Menus> menusList = new ArrayList();
		for(RoleMenus roleMenus:roleMenusList){
			Integer menuId = roleMenus.getMenuId();
			Menus menus = mapper.selectByPrimaryKey(menuId);
			menusList.add(menus);
		}
		return menusList;
	}
}
